function init(){
    if($('.item-list').length>0){
        showPanelUpdate('.item-list', getUpdatePostCateRow);
        submitUpdate(updatePostCateRow);
        changePostCateState();
    } else {
        submitUpdateForm('.btn-submit-update', evtAfterSubmitForm);
    }
}

function getUpdatePostCateRow(row){
    var html = row.parents('table').find('tr.tr-temp').clone().removeClass('hidden').removeClass('tr-temp').addClass('tr-update-temp');
    html.find('#input-post-cate-id').val(row.attr('data-id'));
    html.find('#input-post-cate-title').val(row.find('.input-post-cate-title').html().trim());
    html.find('#input-post-cate-slug').val(row.find('.input-post-cate-slug').attr('data-slug'));
    html.find('#input-post-cate-order').val(row.find('.input-post-cate-order').html());
    html.find('#input-post-cate-delete-flg').prop('checked', row.find('.input-post-cate-delete-flg input[data-name=delete_flg]').is(':checked'));
    return html;
}

function updatePostCateRow(data){
    var trEditting = $('tr.editting');
    var trTempUpdate = $('tr.tr-update-temp');
    var url_template = trEditting.find('td .input-post-cate-slug').attr('data-template');
    trEditting.find('.input-post-cate-title').html(trTempUpdate.find('#input-post-cate-title').val());
    trEditting.find('.input-post-cate-title').attr('href', data.href);
    trEditting.find('.input-post-cate-slug').attr('href', data.href);
    trEditting.find('.input-post-cate-slug').attr('data-slug', trTempUpdate.find('#input-post-cate-slug').val());
    trEditting.find('.input-post-cate-order').html(trTempUpdate.find('#input-post-cate-order').val());
    trEditting.find('.input-post-cate-delete-flg .checkbox').prop('checked', trTempUpdate.find('#input-post-cate-delete-flg').is(':checked'));
}

function changePostCateState(){
    $('.input-post-cate-publish-flg input.checkbox, .input-post-cate-delete-flg input.checkbox').change(function(){
        var url = $(this).parents('table').attr('data-href');
        var value = $(this).is(':checked')==true?1:0;
        var name = $(this).attr('data-name');
        var post_cate = {id: $(this).parents('tr').attr('data-id')};
        post_cate[name]   = value;
        post_cate['case'] = 0;
        changeState(url, {post_cate: post_cate});
    });
}

function evtAfterSubmitForm(data){
    if(data.cmd_case!=undefined && data.cmd_case=='update'){
        if(data.code==1){
            if(document.URL != data.href){
                window.location = data.href;
            }
        }
    } else {
        if(data.code==1){
            if(document.URL != data.href){
                window.location = data.href;
            }
        }
    }
}